//
//  NestViewController.swift
//  JXPagingView
//
//  Created by jiaxin on 2018/11/2.
//  Copyright © 2018 jiaxin. All rights reserved.
//

import UIKit
import JXSegmentedView

class NestViewController: XS_BaseVC {
    var naviDataSource = JXSegmentedTitleDataSource()
    lazy var naviSegmentedView = JXSegmentedView()
    lazy var listContainer = JXSegmentedListContainerView(dataSource: self, type: .scrollView)
    
    lazy var downloadBtn : UIButton = {
        let downloadBtn = UIButton.init(frame: CGRect.init(x: .screenWidth - 44 - 5, y: .barHeight, width: 44, height: 44))
        downloadBtn.setImage(UIImage.init(named: "icon_下载"), for: .normal)
        downloadBtn.addTarget(self, action: #selector(downloadBtnAction), for: .touchUpInside)
        return downloadBtn
    }()
    
    @objc func downloadBtnAction(){
        let downloadVC = XS_DownloadVC()
        navigationController?.pushViewController(downloadVC, animated: true)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.tabBarController?.tabBar.unselectedItemTintColor = #colorLiteral(red: 0.2196078431, green: 0.2352941176, blue: 0.2470588235, alpha: 1)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        let titles = ["学习", "计划"]
        naviDataSource.titles = titles
        naviDataSource.itemSpacing = 24
        naviDataSource.itemWidth = 36
        naviDataSource.titleNormalFont = .systemFont(ofSize: 18, weight: .medium)
        naviDataSource.titleNormalColor = UIColor.qmui_color(withHexString: "#888E93")!
        naviDataSource.titleSelectedColor = UIColor.qmui_color(withHexString: "#383C3F")!

        naviSegmentedView.contentEdgeInsetLeft = 0
        naviSegmentedView.contentEdgeInsetRight = 0
        naviSegmentedView.frame = CGRect(x: 16, y: .barHeight, width: 96, height: 44)
        naviSegmentedView.dataSource = naviDataSource
        
        let lineView = JXSegmentedIndicatorLineView()
        lineView.indicatorColor = UIColor.qmui_color(withHexString: "#2ABD9C")!
        lineView.indicatorWidth = 30
        naviSegmentedView.indicators = [lineView]

        view.addSubview(naviSegmentedView)
        view.addSubview(downloadBtn)

        view.addSubview(listContainer)

        naviSegmentedView.listContainer = listContainer
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        listContainer.frame = CGRect.init(x: 0, y: .barHeight + 44, width: .screenWidth, height: .screenHeight - .bottomHeight - .barHeight - 44)
    }
}

extension NestViewController: JXSegmentedListContainerViewDataSource {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return 2
    }

    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        if index == 0 {
            let pagingVC = NestSubjectViewController()
            pagingVC.nestContentScrollView = listContainer.scrollView
            pagingVC.pagingView.listContainerView.isCategoryNestPagingEnabled = true
            return pagingVC
        }
        let pagingVC = XS_PlanSubVC()
        pagingVC.shouldNavBarHidden = true
//        pagingVC.nestContentScrollView = listContainer.scrollView
//        pagingVC.pagingView.listContainerView.isCategoryNestPagingEnabled = true
        return pagingVC
    }
}
